<template>
  <div class="dashboard-container">
    <div class="dashboard-header">
      <h2 class="dashboard-title">系统概览</h2>
      <div class="dashboard-actions">
        <el-button type="primary" :icon="Refresh" size="small" @click="refreshData" :loading="loading">刷新数据</el-button>
        <el-switch
          v-model="autoRefresh"
          active-text="自动刷新"
          inactive-text="手动刷新"
          @change="toggleAutoRefresh"
        />
      </div>
    </div>
    
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover" class="dashboard-card" v-loading="loading">
          <template #header>
            <div class="card-header">
              <span>项目总数</span>
            </div>
          </template>
          <div class="dashboard-card-content">
            <div class="dashboard-card-value">{{ projectCount }}</div>
            <div class="dashboard-card-icon">
              <span class="el-icon-folder"></span>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card shadow="hover" class="dashboard-card" v-loading="loading">
          <template #header>
            <div class="card-header">
              <span>任务总数</span>
            </div>
          </template>
          <div class="dashboard-card-content">
            <div class="dashboard-card-value">{{ taskCount }}</div>
            <div class="dashboard-card-icon">
              <span class="el-icon-document"></span>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card shadow="hover" class="dashboard-card" v-loading="loading">
          <template #header>
            <div class="card-header">
              <span>待执行任务</span>
            </div>
          </template>
          <div class="dashboard-card-content">
            <div class="dashboard-card-value">{{ pendingTaskCount }}</div>
            <div class="dashboard-card-icon">
              <span class="el-icon-time"></span>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card shadow="hover" class="dashboard-card" v-loading="loading">
          <template #header>
            <div class="card-header">
              <span>已完成任务</span>
            </div>
          </template>
          <div class="dashboard-card-content">
            <div class="dashboard-card-value">{{ completedTaskCount }}</div>
            <div class="dashboard-card-icon">
              <span class="el-icon-check"></span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" class="dashboard-row">
      <el-col :span="12">
        <el-card shadow="hover" class="dashboard-card" v-loading="loading">
          <template #header>
            <div class="card-header">
              <span>最近活动</span>
            </div>
          </template>
          <div class="activity-list">
            <div v-if="recentActivities.length === 0" class="empty-data">
              暂无活动记录
            </div>
            <div v-else v-for="(activity, index) in recentActivities" :key="index" class="activity-item">
              <div class="activity-time">{{ activity.time }}</div>
              <div class="activity-content">{{ activity.content }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="12">
        <el-card shadow="hover" class="dashboard-card" v-loading="loading">
          <template #header>
            <div class="card-header">
              <span>任务进度</span>
            </div>
          </template>
          <div class="task-progress">
            <div v-if="taskProgress.length === 0" class="empty-data">
              暂无任务进度数据
            </div>
            <div v-else v-for="(task, index) in taskProgress" :key="index" class="task-progress-item">
              <div class="task-progress-name">{{ task.name }}</div>
              <el-progress :percentage="task.percentage" :status="task.status"></el-progress>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import dashboardService from '../api/dashboardService'
import { Refresh } from '@element-plus/icons-vue'

export default {
  name: 'Dashboard',
  setup() {
    const projectCount = ref(0)
    const taskCount = ref(0)
    const pendingTaskCount = ref(0)
    const completedTaskCount = ref(0)
    const recentActivities = ref([])
    const taskProgress = ref([])
    const loading = ref(false)
    const autoRefresh = ref(true)
    const refreshTimer = ref(null)
    const refreshInterval = 60000 // 60秒
    
    const fetchDashboardData = async () => {
      loading.value = true
      try {
        const response = await dashboardService.getDashboardData()
        
        if (response.data && response.data.status === 'success') {
          const data = response.data.data
          
          // 更新统计数据
          projectCount.value = data.projectCount || 0
          taskCount.value = data.taskCount || 0
          pendingTaskCount.value = data.pendingTaskCount || 0
          completedTaskCount.value = data.completedTaskCount || 0
          
          // 更新活动列表
          recentActivities.value = data.recentActivities || []
          
          // 更新任务进度
          taskProgress.value = data.taskProgress || []
        } else {
          ElMessage.error('获取仪表盘数据失败')
        }
      } catch (error) {
        console.error('获取仪表盘数据出错:', error)
        ElMessage.error('获取仪表盘数据时发生错误')
      } finally {
        loading.value = false
      }
    }
    
    const refreshData = () => {
      fetchDashboardData()
    }
    
    const toggleAutoRefresh = () => {
      if (autoRefresh.value) {
        startRefreshTimer()
      } else {
        stopRefreshTimer()
      }
    }
    
    const startRefreshTimer = () => {
      stopRefreshTimer() // 先清除可能存在的定时器
      refreshTimer.value = setInterval(() => {
        console.log('自动刷新仪表盘数据...')
        fetchDashboardData()
      }, refreshInterval)
    }
    
    const stopRefreshTimer = () => {
      if (refreshTimer.value) {
        clearInterval(refreshTimer.value)
        refreshTimer.value = null
      }
    }
    
    onMounted(() => {
      fetchDashboardData()
      if (autoRefresh.value) {
        startRefreshTimer()
      }
    })
    
    onUnmounted(() => {
      stopRefreshTimer()
    })
    
    return {
      projectCount,
      taskCount,
      pendingTaskCount,
      completedTaskCount,
      recentActivities,
      taskProgress,
      loading,
      refreshData,
      Refresh,
      autoRefresh,
      toggleAutoRefresh
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  padding: 10px;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.dashboard-title {
  font-size: 20px;
  margin: 0;
  color: #303133;
}

.dashboard-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.dashboard-row {
  margin-top: 20px;
}

.dashboard-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dashboard-card-content {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dashboard-card-value {
  font-size: 32px;
  font-weight: bold;
  color: #409EFF;
}

.dashboard-card-icon {
  font-size: 48px;
  opacity: 0.3;
  color: #409EFF;
}

.activity-list,
.task-progress {
  height: 300px;
  overflow-y: auto;
}

.activity-item {
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.activity-time {
  font-size: 12px;
  color: #999;
  margin-bottom: 5px;
}

.task-progress-item {
  margin-bottom: 15px;
}

.task-progress-name {
  margin-bottom: 5px;
}

.empty-data {
  text-align: center;
  color: #999;
  padding: 40px 0;
}
</style> 